<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>会议管理</title>
    <link rel="stylesheet" href="/static/lib/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <script type="text/html" id="toolbar">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal data-add-btn" lay-event="add"> 添加</button>
                </div>
        </script>

        <!--表格主题-->
        <table class="layui-hide" id="roomTable" lay-filter="roomTable"></table>

        <!--右侧操作-->
        <script type="text/html" id="roomtablehand">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">修改</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="/static/lib/layui/layui.js" charset="utf-8"></script>
<script>


    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        //表格初始化
        var roomTable = table.render({
            elem: '#roomTable',
            url: '/room/list',
            toolbar: '#toolbar',
            response: {
                statusCode: 200 //规定成功的状态码，默认：0
                , countName: 'total' //规定数据总数的字段名称，默认：count
            },
            cols: [
                [
                {field: 'number', width: '7%', title: '会议室编号', align: "center"},
                {field: 'name', width: '10%', title: '会议室名称', align: "center"},
                {field: 'typeName', width: '10%', title: '会议室类型', align: "center"},
                {field: 'place', width: '10%', title: '会议室位置', align: "center"},
                {field: 'count', width: '6%', title: '容纳人数', align: "center"},
                {field: 'empName', width: '8%', title: '管理人员', align: "center"},
                {field: 'empTelephone', width: '8%', title: '联系电话', align: "center"},
                {field: 'description', width: '15%', title: '备注', align: "center"},
                {title: '操作', minWidth: 50, toolbar: '#roomtablehand', align: "center"}
            ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            id: 'roomTableId',
            page: true,
            skin: 'line'
        });

        //自定义方法
        var active = {
            //表格重载
            reloads: function () {
                //执行重载
                roomTable.reload({
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                }, 'data');
            }
        };

        /**
         * 添加与删除
         */
        table.on('toolbar(roomTable)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var open = layer.open({
                    title: '添加会议室',
                    type: 2,
                    shadeClose: true,
                    scrollbar: false,
                    area: ['30%', '60%'],
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    content: '/room/show',
                    yes: function (index, layero) {
                        var body = layer.getChildFrame('body', index);
                        //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.layui.form.on("submit(save)", function (data) {
                            var files = data.field;
                            files['typeName']=iframeWin.typeName;
                            $.ajax({
                                type: "post",
                                url: "/room/save",
                                dataType: "json",
                                data: files,
                                success: function (data) {
                                    layer.closeAll();
                                    layer.msg(data.msg);
                                    active.reloads();
                                },
                                error: function (data) {
                                    layer.closeAll();
                                    layer.msg(data);
                                }
                            });
                            return false;
                        });
                        body.find("#save").click();
                    }
                });
            }
        });

        table.on('tool(roomTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                //修改信息
                var index = layer.open({
                    title: '修改会议室信息',
                    type: 2,
                    shadeClose: true,
                    scrollbar: false,
                    area: ['30%', '60%'],
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    content: '/room/show?id='+data.id,
                    yes: function (index, layero) {
                        var body = layer.getChildFrame('body', index);
                        //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.layui.form.on("submit(save)", function (data) {
                            var files = data.field;
                            files['id']= obj.data.id;
                            files['typeName']=iframeWin.typeName;
                            $.ajax({
                                type: "put",
                                url: "/room/edit",
                                dataType: "json",
                                data: files,
                                success: function (data) {
                                    layer.closeAll();
                                    layer.msg(data.msg);
                                    active.reloads();
                                },
                                error: function (data) {
                                    layer.closeAll();
                                    layer.msg(data);
                                }
                            });
                            return false;
                        });
                        body.find("#save").click();
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        type: "delete",
                        url: "/room/delete",
                        dataType: "json",
                        data: {"id": data.id},
                        success: function (data) {
                            layer.closeAll();
                            layer.msg(data.msg);
                            if (data.success) obj.del();
                        },
                        error: function (data) {
                            layer.closeAll();
                            layer.msg(data);
                        }
                    });
                    layer.close(index);
                });
            }
        });
    });
</script>

</body>
</html>